﻿Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Imports SecretaryToolData

Partial Public Class Reports
	Public Shared Sub TestGenerateReport()

		Dim outputFile As String = "c:\temp\testoutput.pdf"

		Using myDocument As New iTextSharp.text.Document(PageSize.LETTER)
			PdfWriter.GetInstance(myDocument, New FileStream(outputFile, FileMode.Create))

			myDocument.Open()
			myDocument.Add(New Chunk("This is a test"))
			myDocument.Add(New Paragraph("This is a really long piece of text that should be long enough to wrap around the edge of a piece of paper. The idea is to know if I can put a chunk or other text directly after this without having to add additional 'blank' lines."))
			myDocument.Add(New Chunk("This is a test"))
		End Using
	End Sub

	Public Shared Function EmergencyContactInformation(FamilyHeadPublisherID As Long) As FileInfo
		Dim workingData As New VM_EmergencyContactInformation(FamilyHeadPublisherID)
		Dim workingPublisher As New VM_PublisherInformation(FamilyHeadPublisherID)
		Dim outputFile As New FileInfo(String.Format("{0}\{1}-{2}.{3}", ConfigPropertyKeys.AutoBackupLocation, "EmergencyContactInformation", workingData.FullName.AppendFileDateExtension, "pdf"))

		Using myFileStream As New FileStream(outputFile.FullName, FileMode.Create)
			Using myDoc As New Document
				PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim chunkLabel As Chunk
				Dim chunkData As Chunk

				myDoc.Add(New Paragraph(workingData.FullName, fontPageTitle))

				For Each workingFamilyMember As VM_PublisherInformation In workingPublisher.FamilyMembers

					myDoc.Add(New Chunk(Environment.NewLine))
					myDoc.Add(New Paragraph(GetStatusString(workingFamilyMember), fontItem))
					myDoc.Add(New Paragraph(workingFamilyMember.FullName, fontItem))

					myDoc.Add(New Chunk(Environment.NewLine))
					chunkLabel = New Chunk("Mobile Phone: ", fontItemLabel)
					chunkData = New Chunk(workingFamilyMember.MobileTelephone, fontItem)
					myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

					myDoc.Add(New Chunk(Environment.NewLine))
					chunkLabel = New Chunk("Mobile Phone Provider: ", fontItemLabel)
					chunkData = New Chunk(workingFamilyMember.MobileTelephoneProviderName, fontItem)
					myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

					myDoc.Add(New Chunk(Environment.NewLine))
					chunkLabel = New Chunk("Email: ", fontItemLabel)
					chunkData = New Chunk(workingFamilyMember.Email, fontItem)
					myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

					myDoc.Add(New Chunk(Environment.NewLine))
					chunkLabel = New Chunk("Birth Date: ", fontItemLabel)
					chunkData = New Chunk(If(workingFamilyMember.BirthDate.Value.ToLongDateString, "").ToString, fontItem)
					myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

					myDoc.Add(New Chunk(Environment.NewLine))
					chunkLabel = New Chunk("Immersion Date: ", fontItemLabel)
					If workingFamilyMember.ImmersionDate IsNot Nothing Then
						chunkData = New Chunk(If(workingFamilyMember.ImmersionDate.Value.ToLongDateString, "").ToString, fontItem)
					Else
						chunkData = New Chunk("Unknown", fontItem)
					End If
					myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

					myDoc.Add(New Chunk(Environment.NewLine))
				Next

				myDoc.Add(New Chunk(Environment.NewLine))
				myDoc.Add(New Paragraph("Family Head", fontSectionLabel))

				chunkLabel = New Chunk("Home Phone: ", fontItemLabel)
				chunkData = New Chunk(workingData.HomeTelephone, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Work Phone: ", fontItemLabel)
				chunkData = New Chunk(workingData.WorkTelephone, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Physical Address: ", fontItemLabel)
				chunkData = New Chunk(workingData.PhysicalAddress, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Special Needs: ", fontItemLabel)
				chunkData = New Chunk(workingData.SpecialNeeds, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				myDoc.Add(New Chunk(Environment.NewLine))
				myDoc.Add(New Paragraph("Third-Party Contact (preferably out-of-state)", fontSectionLabel))

				chunkLabel = New Chunk("Name: ", fontItemLabel)
				chunkData = New Chunk(workingData.OtherName, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Home Phone: ", fontItemLabel)
				chunkData = New Chunk(workingData.OtherMobileTelephone, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Work Phone: ", fontItemLabel)
				chunkData = New Chunk(workingData.OtherWorkTelephone, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Mobile Phone: ", fontItemLabel)
				chunkData = New Chunk(workingData.OtherMobileTelephone, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

				myDoc.Add(New Chunk(Environment.NewLine))
				chunkLabel = New Chunk("Email: ", fontItemLabel)
				chunkData = New Chunk(workingData.OtherEmail, fontItem)
				myDoc.Add(PhraseChunks(New Chunk() {chunkLabel, chunkData}.ToList))

			End Using
		End Using

		Return outputFile
	End Function

	Private Shared Function GetStatusString(workingPublisher As VM_PublisherInformation) As String
		Dim stats As New List(Of String)

		If workingPublisher.IsAnointed Then stats.Add("Anointed")
		If workingPublisher.IsBlind Then stats.Add("Blind")
		If workingPublisher.IsDeaf Then stats.Add("Deaf")
		If workingPublisher.IsElder Then stats.Add("Elder")
		If workingPublisher.IsFemale Then stats.Add("Female")
		If workingPublisher.IsMale Then stats.Add("Male")
		If workingPublisher.IsMinisterialServant Then stats.Add("Ministerial Servant")
		If workingPublisher.IsPioneer Then stats.Add("Pioneer")
		If workingPublisher.IsPublisher Then
			If workingPublisher.ImmersionDate IsNot Nothing Then
				stats.Add("Baptized Publisher")
			Else
				stats.Add("Unbaptized Publisher")
			End If
		End If
		If workingPublisher.IsRegAuxPioneer Then stats.Add("Regular Auxiliary Pioneer")
		If workingPublisher.IsSpecialPioneer Then stats.Add("Special Pioneer")

		Return String.Join(", ", stats)
	End Function

	Public Shared Function EmergencyContactInformation(FamilyHeadPublisherIDs As List(Of Long)) As FileInfo

		Dim outputFile As New FileInfo(String.Format("{0}\{1}.{2}", ConfigPropertyKeys.AutoBackupLocation, "EmergencyContactInformationAll".AppendFileDateExtension, "pdf"))

		Dim BlackBaseColor As New BaseColor(Color.Black)
		Dim fontPageTitle As New Font(UniCodeBaseFont, 28, Font.BOLD, BlackBaseColor)

		Dim filesToDelete As New List(Of FileInfo)

		Using myFileStream As New FileStream(outputFile.FullName, FileMode.Create)
			Using myDoc As New Document
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent

				myDoc.Add(New Phrase("Congregation Emergency Contact Forms", fontPageTitle))

				For x As Integer = 0 To FamilyHeadPublisherIDs.Count - 1
					Dim FamilyHeadPublisherID As Long = FamilyHeadPublisherIDs(x)
					Dim EmergencyContactInformationFile As FileInfo = EmergencyContactInformation(FamilyHeadPublisherID)
					filesToDelete.Add(EmergencyContactInformationFile)
					Dim reader As New PdfReader(EmergencyContactInformationFile.FullName)
					myDoc.NewPage()
					Dim existingPage As PdfImportedPage = writer.GetImportedPage(reader, 1)
					content.AddTemplate(existingPage, 0, 0)
				Next
			End Using
		End Using

		For Each fileToDelete In filesToDelete
			fileToDelete.Delete()
		Next

		Return outputFile
	End Function
End Class

